package com.lo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hankcs.hanlp.HanLP;
import com.lo.common.FileDeleteUtils;
import com.lo.common.FileExtract;
import com.lo.common.JsonResult;
import com.lo.common.TextualMaterialInfo;
import com.lo.dao.TextualMaterialMapper;
import com.lo.entity.CourseWithTCHName;
import com.lo.entity.Message;
import com.lo.entity.TextualMaterial;
import com.lo.server.WebSocketServer;
import com.lo.service.TextualMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@Service
public class TextualMaterialServiceImpl implements TextualMaterialService {
    @Autowired
    TextualMaterialMapper textualMaterialMapper;
    @Autowired
    private TextualMaterialInfo textualMaterialInfo;
    @Override
    public JsonResult addNewTextualMaterial(TextualMaterial textualMaterial) {
        int rows = textualMaterialMapper.insert(textualMaterial);
        if (rows<=0){
            return new JsonResult(5,"文档资料上传失败",null);
        }
        return new JsonResult(0,"文档资料上传成功",textualMaterial);
    }

    @Override
    public JsonResult deleteTextualMaterial(Long id) {
        List<TextualMaterial> textualMaterials = textualMaterialMapper.selectById(id);
        int rows = textualMaterialMapper.deleteById(id);
        if (rows<=0){
            throw new  RuntimeException("删除失败，请联系管理员");
        }
        FileDeleteUtils.delete(textualMaterialInfo.getBasePath(), textualMaterials.get(0).getUrl());
        return new JsonResult(0,"删除成功",textualMaterials.get(0));
    }

    @Override
    public JsonResult getWordCloud(String path) {
//        System.out.println(path);
        FileExtract fileExtract = new FileExtract();
        String content = null;
        try {
            content = fileExtract.fileExtractText(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> keywordList = HanLP.extractKeyword(content, 10);
//        System.out.println(keywordList);
        return new JsonResult(0,"关键词信息",keywordList);
    }

    @Override
    public JsonResult getTextSummary(String path) {
        FileExtract fileExtract = new FileExtract();
        String content = null;
        try {
            content = fileExtract.fileExtractText(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> sentenceList = HanLP.extractSummary(content, 5);
        return new JsonResult(0,"本文摘要",sentenceList);
    }

    @Override
    public JsonResult updateTextIsDelete(Long id) {
        int rows = textualMaterialMapper.updateTextIsDelete(id);
        if (rows<=0){
            return new JsonResult(1,"更新失败，联系管理员",null);
        }
        return new JsonResult(0,"撤销删除成功，等待管理审核",null);
    }

    @Override
    public JsonResult selectTextById(Long id) {
        List<TextualMaterial> textualMaterials = textualMaterialMapper.selectById(id);
        if (textualMaterials.size()<=0){
            return new JsonResult(1,"获取失败，请联系管理员",null);
        }
        return new JsonResult(0,"获取成功",textualMaterials.get(0));
    }

    @Override
    public JsonResult updateText(TextualMaterial textualMaterial) {
        int rows = textualMaterialMapper.updateText(textualMaterial);
        if (rows<=0){
            return new JsonResult(1,"更新失败，请联系管理员",null);
        }
        return new JsonResult(0,"更新成功",textualMaterial);
    }

    @Override
    public JsonResult selectAllTexts() {
        List<CourseWithTCHName> textualMaterials = textualMaterialMapper.selectAllTexts();
        if (textualMaterials.size()<=0){
            return new JsonResult(1,"课程列表为空",null);
        }
        return new JsonResult(0,"课程列表",textualMaterials);

    }

    @Override
    public JsonResult selectOrdinaryTexts() {
        List<CourseWithTCHName> textualMaterials = textualMaterialMapper.selectOrdinaryTexts();
        if (textualMaterials.size()<=0){
            return new JsonResult(1,"课程列表为空",null);
        }
        return new JsonResult(0,"课程列表",textualMaterials);
    }

    @Override
    public JsonResult selectVipTexts() {
        List<CourseWithTCHName> textualMaterials = textualMaterialMapper.selectVipTexts();
        if (textualMaterials.size()<=0){
            return new JsonResult(1,"课程列表为空",null);
        }
        return new JsonResult(0,"课程列表",textualMaterials);
    }

    @Override
    public PageInfo selectAllTextsWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> course = textualMaterialMapper.selectAllTextsWithPage(search);
       PageInfo pageInfo = new PageInfo<>(course);
       return pageInfo;

    }

    @Override
    public Object selectOrdinaryTextsWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> course = textualMaterialMapper.selectOrdinaryTextsWithPage(search);
        PageInfo pageInfo = new PageInfo<>(course);
        return pageInfo;

    }

    @Override
    public Object selectVipTextsWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> course = textualMaterialMapper.selectVipTextsWithPage(search);
        PageInfo pageInfo = new PageInfo<>(course);
        return pageInfo;
    }

    @Override
    public Object selectEngineeringTextWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> course = textualMaterialMapper.selectEngineeringTextWithPage(search);
        PageInfo pageInfo = new PageInfo<>(course);
        return pageInfo;
    }

    @Override
    public Object selectScienceTextWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> course = textualMaterialMapper.selectScienceTextWithPage(search);
        PageInfo pageInfo = new PageInfo<>(course);
        return pageInfo;
    }

    @Override
    public Object selectLiteratureTextWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> course = textualMaterialMapper.selectLiteratureTextWithPage(search);
        PageInfo pageInfo = new PageInfo<>(course);
        return pageInfo;
    }

    @Override
    public Object selectLawTextWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> course = textualMaterialMapper.selectLawTextWithPage(search);
        PageInfo pageInfo = new PageInfo<>(course);
        return pageInfo;
    }

    @Override
    public JsonResult selectAllTextual(int pageNum, int pageSize, String search, String controllInfo) {

        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> courseWithTCHNameList = new ArrayList<>();
//        System.out.println("textual controll is "+controllInfo);
        switch (controllInfo){
            case "2":
                //所有审核过的课程
                courseWithTCHNameList=textualMaterialMapper.selectAllTextsWithPage(search);
                break;
            case "4":
                //所有未审核的课程
                courseWithTCHNameList=textualMaterialMapper.selectAllNotExamineTextual(search);
                break;
            case "6":
                //所有待删除课程
                courseWithTCHNameList = textualMaterialMapper.selectAllDeletingTextual(search);
                break;
        }
//        System.out.println(courseWithTCHNameList);
        PageInfo pageInfo = new PageInfo(courseWithTCHNameList);
        return new JsonResult(0,"",pageInfo);
    }

    @Override
    public JsonResult deleteTextual(Long id) {
        List<TextualMaterial> textual = textualMaterialMapper.selectById(id);
        int rows = textualMaterialMapper.deleteById(id);
        String tid = textual.get(0).getTid().toString();
        if(rows <= 0){
            throw new  RuntimeException("删除失败，请联系管理员");
        }
        Message message = new Message(0,"message","文档已被删除，请查看");
        WebSocketServer.sendMessageByUserId(tid,message);
        return new JsonResult(0,"删除成功",textual.get(0));

    }

    @Override
    public JsonResult updateTextExamine(Long id) {
        List<TextualMaterial> textualMaterials = textualMaterialMapper.selectById(id);
        String tid = textualMaterials.get(0).getTid().toString();
        Message message = new Message(0,"message","文档通过审核");
        WebSocketServer.sendMessageByUserId(tid,message);
        return new JsonResult<>(0,"",textualMaterialMapper.updateTextExamine(id));
    }

    @Override
    public JsonResult updateTextIsDelete2Zero(Long id) {
        int rows = textualMaterialMapper.updateTextIsDelete2Zero(id);
        List<TextualMaterial> textualMaterials = textualMaterialMapper.selectById(id);
        String tid = textualMaterials.get(0).getTid().toString();
        if (rows<=0){
            return new JsonResult<>(1,"为撤销成功",null);
        }
        Message message = new Message(0,"message","文档被撤销删除，请查看");
        WebSocketServer.sendMessageByUserId(tid,message);
        return new JsonResult<>(0,"撤销成功",null);
    }

    @Override
    public JsonResult selectAllTextsToWX(String search) {
        return new JsonResult<>(0,"文本总览",textualMaterialMapper.selectAllTextsWithPage(search)) ;
    }


}
